<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: base/store/users.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: base/store/users.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 *  users store module
 *  @module base/store/users
 *  @author coder
 */
import {
  FETCH_USERS,
  GET_USERS,
  ADD_USERS,
  UPDATE_USERS,
  REMOVE_USERS,
  BATCH_USERS
} from '@/base/types'
import {
  fetchUsers,
  getUsers,
  addUsers,
  updateUsers,
  removeUsers,
  batchUsers
} from '@/base/api/users'

// store module
export default {

  /**
   * 状态数据
   * @member state
   * @property {Array} list 获取users列表响应数据
   * @property {Number} page 获取users列表响应页码
   * @property {Number} limit 获取users列表页大小，每页记录条数
   * @property {Number} total 获取users列表数据记录总条数
   * @property {Object} model 获取users单条记录
   */
  state: {
    list: [],
    page: null,
    limit: null,
    total: 0,
    model: null
  },
  mutations: {

    /**
     * 获取users列表
     * @function [mutations]FETCH_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {Array} [payload.list] 数据列表
     * @param {number} [payload.page=0] 数据页码
     * @param {number} [payload.limit=0] 每页条数
     * @param {number} [payload.total=0] 总记录数
     */
    [FETCH_USERS](state, payload) {
      state.list = payload.list || [];
      state.page = payload.page || 0;
      state.limit = payload.limit || 0;
      state.total = payload.total || 0;
    },
    /**
     * 获取users单条记录
     * @function [mutations]GET_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {object} payload.model 实体对象
     */
    [GET_USERS](state, payload) {
      state.model = payload.model
    },
    /**
     * 新增users
     * @function [mutations]ADD_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {number} [payload.index] 新增到列表的位置索引，默认最近到列表最后
     * @param {object} payload.model 新增的实体对象
     */
    [ADD_USERS](state, payload) {
      if (payload.index !== undefined) {
        state.list.splice(payload.index, 0, payload.model)
      } else {
        state.list.push(payload.model);
      }
      state.total += 1
    },
    /**
     * 更新users
     * @function [mutations]UPDATE_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {number} [payload.index] 源实体在列表的位置索引，如没有index，即不更新源列表数据
     * @param {object} payload.model 更新的实体对象
     */
    [UPDATE_USERS](state, payload) {
      if (payload.index !== undefined) {
        state.list.splice(payload.index, 1, payload.model)
      }
    },
    /**
     * 删除users
     * @function [mutations]REMOVE_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {number} [payload.index] 源实体在列表的位置索引，如没有index，即不删除实体在源列表数据
     */
    [REMOVE_USERS](state, payload) {
      if (payload.index !== undefined) {
        state.list.splice(payload.index, 1)
      }
      state.total -= 1
    },
    /**
     * 批量删除users
     * @function [mutations]BATCH_USERS
     * @param {object} state vuex状态
     * @param {object} payload 负载数据
     * @param {number[]} [payload.indexes] 源实体在列表的位置索引，如没有index，即不删除实体在源列表数据
     */
    [BATCH_USERS](state, payload) {
      state.list = state.list.filter((item, index) => !payload.indexes.includes(index));
      state.total -= payload.indexes.length
    }
  },
  actions: {

    /**
     * 获取users列表
     * @function [actions]FETCH_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {object} payload.data 请求发送数据data
     * @returns {Promise} Promise实例
     */
    [FETCH_USERS]({
      commit
    }, {
      data
    } = {}) {
      return fetchUsers(data).then(res => {
        commit({
          type: FETCH_USERS,
          list: res.list,
          page: Number.parseInt(res.page),
          limit: Number.parseInt(res.limit),
          total: Number.parseInt(res.total)
        })
        return res
      })
    },
    /**
     * 获取users单条记录
     * @function [actions]GET_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {string|number} payload.id  URL路径参数数据id
     * @param {object} payload.data 请求发送数据data
     * @returns {Promise} Promise实例
     */
    [GET_USERS]({
      commit
    }, {
      id,
      data
    } = {}) {
      return getUsers(id, data).then(res => {
        commit({
          type: GET_USERS,
          model: res
        })
        return res
      })
    },
    /**
     * 新增users
     * @function [actions]ADD_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {object} payload.data 请求发送数据data
     * @param {number} [payload.index] 新增到列表的位置索引，默认最近到列表最后
     * @returns {Promise} Promise实例
     */
    [ADD_USERS]({
      commit
    }, {
      data,
      index
    } = {}) {
      return addUsers(data).then(res => {
        commit({
          type: ADD_USERS,
          model: Object.assign({}, data, res),
          index: index
        })
        return res
      })
    },
    /**
     * 更新users
     * @function [actions]UPDATE_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {object} payload.data 请求发送数据data
     * @param {number} [payload.index] 源实体在列表的位置索引，如没有index，即不更新实体在源列表数据
     * @returns {Promise} Promise实例
     */
    [UPDATE_USERS]({
      commit,
      state
    }, {
      data,
      index
    } = {}) {
      return updateUsers(data).then(res => {
        commit({
          type: UPDATE_USERS,
          index: index,
          model: Object.assign({}, state.list[index], data, res)
        })
        return res
      })
    },
    /**
     * 删除users
     * @function [actions]REMOVE_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {string|number} payload.id  URL路径参数数据id
     * @param {object} payload.data 请求发送数据data
     * @param {number} [payload.index] 源实体在列表的位置索引，如没有index，即不删除实体在源列表数据
     * @returns {Promise} Promise实例
     */
    [REMOVE_USERS]({
      commit
    }, {
      id,
      data,
      index
    } = {}) {
      return removeUsers(id, data).then(res => {
        commit({
          type: REMOVE_USERS,
          index: index
        })
        return res
      })
    },
    /**
     * 批量删除users
     * @function [actions]BATCH_USERS
     * @param {object} context Store实例
     * @param {function} context.commit 提交mutation方法
     * @param {object} payload 负载数据对象
     * @param {object} payload.data 请求发送数据data
     * @param {number[]} [payload.indexes] 源实体在列表的位置索引，如没有indexes，即不删除实体在源列表数据
     * @returns {Promise} Promise实例
     */
    [BATCH_USERS]({
      commit
    }, {
      data,
      indexes
    } = {}) {
      return batchUsers(data).then(res => {
        commit({
          type: BATCH_USERS,
          indexes: indexes || []
        })
        return res
      })
    }
  }
}
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-base_api_users.html">base/api/users</a></li><li><a href="module-base_store_users.html">base/store/users</a></li><li><a href="module-base_types.html">base/types</a></li><li><a href="module-mapping_index.html">mapping/index</a></li><li><a href="module-mock_templates.html">mock/templates</a></li><li><a href="module-router_index.html">router/index</a></li><li><a href="module-store_getters.html">store/getters</a></li><li><a href="module-store_index.html">store/index</a></li><li><a href="module-store_mutations.html">store/mutations</a></li><li><a href="module-store_state.html">store/state</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sat Oct 13 2018 10:32:51 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
